if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}
function plusReady() {

    var user = userLogin();
    //	console.log(JSON.stringify(user));
    InitPage(user);

    BindEvent(user);

}

function InitPage(user) {

    var requestdata = { "userid": user.id };
    $.ajax({
        type: "GET",
        url: configManager.RequstUrl + "api/user/collections",
        async: true,
        data: requestdata
    }).done(function (data) {

        console.log(JSON.stringify(data));
        if ("success" == data.state) {

            PrintPage(data.data);

        } else {
            plus.ui.alert(data.message, function () { }, configManager.alerttip, configManager.alertCtip);
        }
    }).fail(function () {
        plus.nativeUI.alert(errorMessage.interface);
    });
}

function PrintPage(data) {
    var html = [];
    if (data.length > 0) {
        $.each(data, function (i, data) {
            var src = configManager.storeImgurl.format(data.store.store_pic, "");
            html.push('<p id="item' + data.store.id + '"><a class="cancel" ><span name="btnCancle" tip=' + data.store.id + '>取消关注</span><b><img src="' + src + '"></b>' + data.store.store_name + '</a></p>');
        });
        $("#collections").html(html.join(""));
    } else {
        var html = [];
        html.push('<div class="ktxr">');
        html.push('<div style="" class="bg-f2" id="content2">');
        html.push('<div style="margin-top:5%;margin-bottom:5%;"></div>');
        html.push('<img style="margin-left:34%;width:120px;height:120px;" src="../../images/ktxr.gif"><p style="margin-left:34%;">您还没有关注商家哟！</p>');
        html.push('</div>');
        html.push('</div>');
        $("#collections").html(html.join(""));
    }
}

function BindEvent(user) {
    $("#collections").on("click", "span[name=btnCancle]", function () {
        var this_a = this;
        var store_id = $(this).attr("tip");
        plus.nativeUI.confirm("取消关注该商家？", function (e) {
            if (e.index == 0) {
                $("#item" + store_id + "").hide();
                var postdata = { "userid": user.id, "token": user.token, "store": store_id };
                $.ajax({
                    type: "POST",
                    url: configManager.RequstUrl + "api/store/delcollect",
                    async: true,
                    data: postdata
                }).done(function (data) {
                    if ("success" == data.state) {
                       
                    }
                });

            }
        }, "提示", ["确认", "返回"]);



    })
}
